package sieve

import (
	"context"
	"github.com/google/wire"
	"sieve_admin_server_userpass/app/api/util"
	"sieve_admin_server_userpass/app/contextx"
	"sieve_admin_server_userpass/app/schema"
	"sieve_admin_server_userpass/app/server"
	serviceSieve "sieve_admin_server_userpass/app/service/sieve"
)

var _ util.API = (*EngineAPI)(nil)

var EngineSet = wire.NewSet(wire.Struct(new(EngineAPI), "*"))

type EngineAPI struct {
	util.EmptyAPI

	SieveEngineSrv *serviceSieve.SieveEngineSrv
}

func (a *EngineAPI) GetApis() []*server.BatchModTree {
	return []*server.BatchModTree{
		{
			Obj:   a,
			Name:  "engine",
			Title: "引擎任务管理", Perms: []*server.Perm{
				{Name: "query", Title: "查询"},
				{Name: "add", Title: "新增"},
				{Name: "download", Title: "下载"},
				{Name: "operate", Title: "引擎操作"},
			},
			Order: 6,
		},
	}
}

func (a *EngineAPI) API_query_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("state", true),
				contextx.NewVar("detail", true),
			},
		)

		p = schema.PaginationParam{
			PageIndex: util.ToInt(ctx.Args.Get("page_index")),
			PageSize:  util.ToInt(ctx.Args.Get("page_size")),
		}
	)

	return a.SieveEngineSrv.Query(context.Background(), reqArgs, p)
}

func (a *EngineAPI) API_operate_items(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("op"),
				contextx.NewVar("number", true),
				contextx.NewVar("task_id"),
				contextx.NewVar("from_task_id"),
				contextx.NewVar("to_task_id"),
				contextx.NewVar("before_or_after", true),
			},
		)
	)
	return a.SieveEngineSrv.Operate(context.Background(), reqArgs)
}
